﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MINERP.NewPB.Forms
{
    public class DXFHelper
    {
        public static List<string> OrderDxfSelect(List<string> inDirList, string inStandStr)
        {
            List<string> rtnList = new List<string>();
            string standfileName = "";
            int fileIndex = inStandStr.LastIndexOf("\\");
            if (fileIndex < 0)
            {
                standfileName = inStandStr;
            }
            else
            {
                standfileName = inStandStr.Substring(fileIndex);

            }

            foreach (string path in inDirList)
            {
                string fileName = "";
                int fileIndexC = path.LastIndexOf("\\");
                int fileIndexE = path.LastIndexOf(".");
                if (fileIndexC < 0)
                {
                    fileIndexC = 0;
                }
                if (fileIndexE <= 0)
                {
                    fileIndexE = path.Length;
                }
                else
                {
                    fileIndexE = fileIndexE - 1;// 出去.
                }
                fileName = path.Substring(fileIndexC + 1, fileIndexE - fileIndexC);
                if (standfileName.IndexOf(fileName) > 0)
                {
                    rtnList.Add(path);
                }

            }
            return rtnList;

        }
        public static List<string> OrderDxfItem(List<string> inDirList, string inStandStr)
        {
            List<string> rl = new List<string>();
            Dictionary<string, int> resultDict = new Dictionary<string, int>();
            string standStr = Path.GetFileNameWithoutExtension(inStandStr);
            List<string> standList = AnalyserStr(standStr);

            //standList.ForEach(ea => WriteLogForCheck.WriteLogFile(ea));
            foreach (string dirStr in inDirList)
            {
                string dir = Path.GetFileNameWithoutExtension(dirStr);
                List<string> dirList = AnalyserStr(dir);
                var r = dirList.Intersect(standList);
                resultDict.Add(dirStr, r.Count());
            }

            var rd = resultDict.OrderByDescending(or => or.Value);
            foreach (KeyValuePair<string, int> keyValuePair in rd)
            {
                rl.Add(keyValuePair.Key);
            }
            //WriteLogForCheck.WriteLogFile(rl.Count().ToString());
            return rl;
        }

        public static List<string> AnalyserStr(string instr)
        {
            List<string> rtnList = new List<string>();
            //Regex rx = new Regex(@"[a-z]{2,}", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            //string regexStr = @"\w+";
            //string srcString = instr;
            //Regex r = new Regex(regexStr, RegexOptions.None);
            //var result = rx.Matches(srcString);
            var result = instr.ToCharArray();
            foreach (var r1 in result)
            {
                rtnList.Add(r1.ToString());
            }
            return rtnList;
        }

        public static List<string> GetDxfFile(string inPath)
        {
            List<string> rtnList = new List<string>();
            if (System.IO.Directory.Exists(inPath))
            {
                rtnList = System.IO.Directory.GetFiles(inPath, "*.dxf", SearchOption.AllDirectories).ToList();
            }
            else
            {
                return new List<string>();
            }
            return rtnList;
        }

        public static string MathFile(List<string> dxfFileList, string inStandStr)
        {
             var  path=OrderDxfSelect(dxfFileList, inStandStr);
             if (path.Count > 0)
             {
                 return path.FirstOrDefault();
             }
             else
             {
                 return "";
             }
        }
        public static byte[] GetDXFContent(string  path)
        {
            byte[] data;
            using (FileStream fs = File.OpenRead(path))
            {
                data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
            }
            return data;
        }
    }
}
